package com.liaoban.app.ui.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.liaoban.app.R;
import com.liaoban.app.api.type.ShareContent;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

/**
 * <p>分享<p/>
 * Created by ybc on 17/9/20.
 */
public abstract class BaseShareActivity extends BaseToolbarActivity {
    private PopupWindow popupWindow;
    private ShareAction shareAction;
    private TextView tvYaoqingCode;

    private void showShare(View parent, int yaoQingCode) {
        if (popupWindow == null) {
            LayoutInflater lay = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = lay.inflate(R.layout.pop_share, null);
            popupWindow = new PopupWindow(v, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,
                    true);
            ImageView ivClose = v.findViewById(R.id.iv_close);
            ivClose.setOnClickListener(v1 -> popupWindow.dismiss());
            View wx = v.findViewById(R.id.layout_wx);
            wx.setOnClickListener(v12 -> postShare(SHARE_MEDIA.WEIXIN));
            View wxCircle = v.findViewById(R.id.layout_wx_circle);
            wxCircle.setOnClickListener(v15 -> postShare(SHARE_MEDIA.WEIXIN_CIRCLE));
            View qq = v.findViewById(R.id.layout_qq);
            qq.setOnClickListener(view -> postShare(SHARE_MEDIA.QQ));
            View qqCircle = v.findViewById(R.id.layout_qq_circle);
            qqCircle.setOnClickListener(view -> postShare(SHARE_MEDIA.QZONE));
            View sina = v.findViewById(R.id.layout_sina);
            sina.setOnClickListener(view -> postShare(SHARE_MEDIA.SINA));
            tvYaoqingCode = v.findViewById(R.id.tv_yaoqing_code);
            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            popupWindow.setFocusable(true);
            popupWindow.setOutsideTouchable(true);
        }
        tvYaoqingCode.setText(String.valueOf(yaoQingCode));
        popupWindow.update();
        popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
    }

    public void postShare(SHARE_MEDIA share_media) {
        popupWindow.dismiss();
        shareAction.setPlatform(share_media).setCallback(umShareListener).share();
    }

    private void setShareInfo(Activity activity, String title, String content, String targetUrl, int drawableRes) {
        UMWeb web = new UMWeb(targetUrl);
        web.setTitle(title);
        web.setThumb(new UMImage(this, drawableRes));
        web.setDescription(content);
        shareAction = new ShareAction(activity);
        shareAction.withText(content);
        shareAction.withMedia(web);
    }

    private void setShareInfo(Activity activity, String title, String content, String targetUrl, String iconUrl) {
        UMWeb web = new UMWeb(targetUrl);
        web.setTitle(title);
        web.setThumb(new UMImage(this, iconUrl));
        web.setDescription(content);
        shareAction = new ShareAction(activity);
        shareAction.withText(content);
        shareAction.withMedia(web);
    }

    /**
     * 分享监听器
     */
    private UMShareListener umShareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {
            Log.e("yubch", platform.getName() + " onStart");
            showRunningDialog();
        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            dismissRunningDialog();
            Log.e("yubch", platform.getName() + " success");
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            dismissRunningDialog();
            Log.e("yubch", "onError:" + t.getMessage());
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            dismissRunningDialog();
            Log.e("yubch", platform.getName() + " onCancel");
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e("yubch", " onActivityResult");
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onResume() {
        super.onResume();
        dismissRunningDialog();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UMShareAPI.get(this).release();
    }

    /**
     * 分享
     *
     * @param parent
     */
    public void share(View parent, ShareContent shareContent) {
        setShareInfo(this, shareContent.getTitle(), shareContent.getContent(), shareContent.getUrl(), /*shareContent.getImg()*/R.mipmap.ic_logo);
        showShare(parent, shareContent.getYaoqingCode());
    }
}
